package  
{
    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.*;
    import flash.net.URLRequest;
    import flash.system.*;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.utils.Timer;
    
    import mx.events.FlexEvent;
    import mx.preloaders.IPreloaderDisplay;
    import mx.preloaders.Preloader;	


    public class CustomProgress extends Sprite implements IPreloaderDisplay
    {
	   	public var request:URLRequest;
	   	public var loader:Loader;    	
        private var _bgAlpha:Number;
        private var _bgColor:uint;
        private var _bgImage:Object;
        private var _bgSize:String;
        private var _stageHeight:Number;
        private var _stageWidth:Number;
        private var _preloader:Preloader;

        private var _downloadBar:Shape;
        private var _initBar:Shape;
        private var _initField:TextField;
        
        [Bindable]
        public var message:String;	 
        [Bindable]
        public var vx:Number = 0.;
        [Bindable]
        public var vy:Number =0.;                        
        
        private var mc:MovieClip = null;

        public function CustomProgress()
        {
            _initField = new TextField();
            _initField.defaultTextFormat =
                new TextFormat( 'Arial', 12, 0x000000, true );
			
            _downloadBar = new Shape();
            addChild( _downloadBar );

            _initBar = new Shape();
            addChild( _initBar );
             this._bgColor = 0x83DCFF;
             this.backgroundColor = 0x83DCFF;
            initialize();
        
            
        }

        // initialize any properties on display.
        public function initialize():void
        {
        	
            _downloadBar.x = ( _stageWidth / 2 ) - 20;
            _initBar.x = _downloadBar.x - 2;
            _downloadBar.y = ( _stageHeight / 2 ) - 50;
            _initBar.y = _downloadBar.y;
            _initField.x = _initBar.x-30;
            _initField.y = _initBar.y + 100 ;
  			_initField.text = "loading..................";            
            
            addChild( _initField );                 
        }
        // define event handlers of the Preloader instance
        public function set preloader( obj:Sprite ):void
        {

            _preloader = obj as Preloader;

            _preloader.addEventListener( ProgressEvent.PROGRESS,
                                            downloadProgressHandler2 );
            _preloader.addEventListener( FlexEvent.INIT_PROGRESS,
                                            initProgressHandler );
            _preloader.addEventListener( FlexEvent.INIT_COMPLETE,
                                            initCompleteHandler );
                               		    
		    loader = new Loader();


			request = new URLRequest("http://www.shui888.com/loading.swf");
			//request = new URLRequest("http://www.naoxin.net/loading.swf");
			//request = new URLRequest("http://www.shui888.com/loading.swf");
			//request = new URLRequest("http://211.161.1.50/flash/loading.swf");
			//request = new URLRequest("http://ppcar.limewebs.com/loading.swf");
			//request = new URLRequest("loading.swf");
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,handleComplete);
			loader.contentLoaderInfo.addEventListener( SecurityErrorEvent.SECURITY_ERROR,handleSecurityError );
            loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS,
                                            downloadProgressHandler );			
			loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, handleIOError );

			loader.load(request);	
        }

        public function get backgroundAlpha():Number
        {
            return _bgAlpha;
        }
        public function set backgroundAlpha(value:Number):void
        {
            _bgAlpha = value;
        }

        public function get backgroundColor():uint
        {
            return _bgColor;
        }
        public function set backgroundColor(value:uint):void
        {
            _bgColor = value;
        }

        public function get backgroundImage():Object
        {
            return _bgImage;
        }
        public function set backgroundImage(value:Object):void
        {
            _bgImage = value;
        }

        public function get backgroundSize():String
        {
            return _bgSize;
        }
        public function set backgroundSize(value:String):void
        {
            _bgSize = value;
        }

        public function get stageHeight():Number
        {
            return _stageHeight;
        }
        public function set stageHeight(value:Number):void
        {
            _stageHeight = value;
        }

        public function get stageWidth():Number
        {
            return _stageWidth;
        }

        public function set stageWidth(value:Number):void
        {
            _stageWidth = value;
        }

        // handle SWF file download progress.
        private function downloadProgressHandler( evt:ProgressEvent ):void
        {
            var perc:Number = ( ( evt.bytesLoaded / evt.bytesTotal ) * 100 );
            var top:Number = 100 - perc;
        
            _downloadBar.graphics.clear();
            _downloadBar.graphics.beginFill( 0xFF0000, 1 );
            _downloadBar.graphics.moveTo( 0, 0 );
            _downloadBar.graphics.lineTo( 10, 0 );
            _downloadBar.graphics.lineTo( 10, perc * 0.9 );
            _downloadBar.graphics.lineTo( 0, perc * 0.9 );
            _downloadBar.graphics.lineTo( 0, 0 );
            _downloadBar.graphics.endFill();
      
        }
        
        private function downloadProgressHandler2( evt:ProgressEvent ):void
        {
            var perc:Number = ( ( evt.bytesLoaded / evt.bytesTotal ) * 100 );

			this.message = int(perc) +"%";
			_initField.text = message;
			vx = 1.1+(9.9-1.1)*perc/100.;
			vy = 32.5+(-41.3-32.5)*perc/100.;  
			if(mc){
				mc.pValue.text = message;
           		 //mc.mask_mc.m_heart_txt.mask_m.x = vx;
           		 //mc.mask_mc.m_heart_txt.mask_m.y = vy;					
			}
            
        }        
        
 	    public function handleIOError(event:IOErrorEvent ):void {
			trace("click start5");
		}
		
		public function handleSecurityError( event:SecurityErrorEvent ):void {
			
			trace( "Load failed: Security Error: " + event.text );
		}
		
	    public function handleComplete(event:Event):void{
	    	//var comp:UIComponent= new UIComponent();
	    	//comp.addChild(loader);
	    	mc = event.currentTarget.content as MovieClip;
	    	addChild(mc);
	    	//mc.pValue.text = "test";
	    	mc.pValue.text = message;
	    	mc.x = 280;
	    	mc.y = 100;   	    	

            			
			this.removeChild(_downloadBar);
			this.removeChild(this._initBar);
			
			/*
            mc.mask_mc.m_heart_txt.mask_b.mask = mc.mask_mc.m_heart_txt.mask_m;
            mc.mask_mc.m_heart_txt.mask_m.x = vx;
            mc.mask_mc.m_heart_txt.mask_m.y = vy;	   
            */              	
	    }       
        
        // handle application initialization progress.
        private function initProgressHandler( evt:FlexEvent ):void
        {
            _initField.text = "initializing................";
        }
        // handle completion of download and initialization.
        private function initCompleteHandler( evt:FlexEvent ):void
        {
            var timer:Timer = new Timer( 2000, 1 );
            timer.addEventListener( TimerEvent.TIMER_COMPLETE, notifyOfComplete );
            timer.start();
        }
        // notify of completion of download and initialization.
        private function notifyOfComplete( evt:TimerEvent ):void
        {
            dispatchEvent( new Event( Event.COMPLETE ) );
        }
    }

}